package com.zw.pdm.module.report.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zw.pdm.common.ImportCallBack;
import com.zw.pdm.constant.Constant;
import com.zw.pdm.entity.rep.Plan;
import com.zw.pdm.entity.rep.Sheet;
import com.zw.pdm.module.report.mapper.PlanMapper;
import com.zw.pdm.module.report.model.AssPlanImportModel;
import com.zw.pdm.module.report.repository.PlanRepository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.zw.pdm.util.BeanUtil.copy;

/**
 * @Description: 外协件导入
 *
 * @Author: China.sgl
 * @Date: 2020/8/12 15:17
 */
@Deprecated
public class AssPlanImportListener extends AnalysisEventListener<AssPlanImportModel> {

    private Sheet sheet;
    private PlanRepository planRepository;
    private PlanMapper planMapper;
    private ImportCallBack callBack;
    private List<AssPlanImportModel> outPlanImportModels = new ArrayList<>();
    private List<Integer> exceptionList = new ArrayList<>();
    private int i = 2;

    public AssPlanImportListener(Sheet sheet, PlanRepository planRepository,
                                 PlanMapper planMapper,
                                 ImportCallBack callBack) {
        this.sheet = sheet;
        this.planRepository = planRepository;
        this.planMapper = planMapper;
        this.callBack = callBack;
    }

    @Override
    public void invoke(AssPlanImportModel assPlanImportModel, AnalysisContext analysisContext) {
        if (assPlanImportModel.getMalDesc() == null) {
            assPlanImportModel.setReason(assPlanImportModel.getReason() + "物料描述没填");
        }
        if (assPlanImportModel.getDiagram() == null) {
            assPlanImportModel.setReason(assPlanImportModel.getReason() + "图号没填");
        }
        if (assPlanImportModel.getNum() == null) {
            assPlanImportModel.setReason(assPlanImportModel.getReason() + "数量没填");
        }
        if (assPlanImportModel.getUnit() == null) {
            assPlanImportModel.setReason(assPlanImportModel.getReason() + "单位没填");
        }
        if (assPlanImportModel.getAssistType() == null) {
            assPlanImportModel.setReason(assPlanImportModel.getReason() + "外协类型没填");
        }

        assPlanImportModel.setSheetId(sheet.getId());
        assPlanImportModel.setCreateTime(new Date());
        assPlanImportModel.setStatus(Constant.STATUS_NORMAL);

        Plan plan = new Plan();
        copy(assPlanImportModel, plan);

        /*if (planMapper.CountByCondition(plan) > 0) {
            assPlanImportModel.setReason(assPlanImportModel.getReason() + "重复数据");
        }*/

        // 验证
        if (assPlanImportModel.getReason().length() > 0) {
            outPlanImportModels.add(assPlanImportModel);
            i++;
            return;
        }

        planRepository.save(plan);
        i++;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (exceptionList.size() > 0) {
            AssPlanImportModel model = new AssPlanImportModel();
            model.setReason( "第" + exceptionList.toString() + "行数据格式不正确");
            outPlanImportModels.add(model);
        }
        if (outPlanImportModels.size() > 0) {
            callBack.onWarning(outPlanImportModels);
        } else {
            callBack.onSuccess();
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exceptionList.add(i);
        i++;
    }
}
